package com.zcm.modules.cxf.interceptor;

import com.zcm.modules.cxf.utils.WebServiceHeaderUtil;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.headers.Header;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.jaxb.JAXBDataBinding;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WebServiceRequestHeaderParser  extends AbstractPhaseInterceptor<SoapMessage> {
    public WebServiceRequestHeaderParser(){
        //super(Phase.POST_UNMARSHAL);
        super(Phase.INVOKE);
    }
    @Override
    public void handleMessage(SoapMessage soapMsg) throws Fault {
        System.out.println("服务器端--请求头解析器");
        Header xx = soapMsg.getHeader(new QName("header", "http://sechemas.services.cxf.modules.zcm.com/header/"));
        List<Header> headers = soapMsg.getHeaders();
        try {
            Map<String, String> envMap = new HashMap<String, String>();
            envMap.put( "soapenv" , "http://services.cxf.modules.zcm.com/");
            Map<String, String> nsMap = new HashMap<String, String>();
            nsMap.put( "http://services.cxf.modules.zcm.com/" , "ser" );
            JAXBDataBinding dataBinding = (JAXBDataBinding)soapMsg.getExchange().getEndpoint().getService().getDataBinding();
            dataBinding.setNamespaceMap(nsMap);
            soapMsg.put("soap.env.ns.map", envMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
//        Header header=null;
//        QName operation = (QName) soapMsg.getContextualProperty( "javax.xml.ws.wsdl.operation");
//        if("login".equals(operation.getLocalPart())){
//            return;
//        }
//        if(headers==null||headers.size()==0){
//            throw new Fault(new Exception("没有检查到请求头"));
//        }else{
//            header=headers.get(0);
//        }
//        Element element=(Element) header.getObject();
//        NodeList idNodeList = element.getElementsByTagName(WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_ID);
//        NodeList timeNodeList = element.getElementsByTagName(WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_TIME);
//        NodeList codeNodeList = element.getElementsByTagName(WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_CODE);
//        if(idNodeList==null||idNodeList.getLength()==0){
//            throw new Fault(new Exception("没有找到"+WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_ID));
//        }else if(timeNodeList==null||timeNodeList.getLength()==0){
//            throw new Fault(new Exception("没有找到"+WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_TIME));
//        }else if(codeNodeList==null||codeNodeList.getLength()==0){
//            throw new Fault(new Exception("没有找到"+WebServiceHeaderUtil.REQUEST_HEADER_TAGETNAME_CODE));
//        }
//        String request_id=idNodeList.item(0).getTextContent();
//        String request_time=timeNodeList.item(0).getTextContent();
//        String request_code=codeNodeList.item(0).getTextContent();
//        boolean validateFlag = WebServiceHeaderUtil.validateHeader(request_id,request_time,request_code);
//        if(!validateFlag ){
//            throw new Fault(new Exception("请求头信息无效"));
//        }
//        System.out.println("=========================================================");

    }
}